<?php
/*
* Author: Jason ZHENG <zmj522008@hotmil.com>
* Date  : 2018/06/08
* Class : Product 品牌类型系列数据导入
*/
namespace app\index\controller\command;

use think\Controller;
use think\Db;
use think\Session;
use think\Request;

class Product extends Controller
{ 

	public function index()
	{   
		echo 'Product Command Controller';
	}

	public function setProductFullname()
	{
		$products =  Db::table('product')
				->where(['state'=>0])
				->select();

		foreach ($products as $key => $product) 
		{
			$product_name = $this->set_product_name($product['systematics_id']);

			$full_name = $this->getProductConditionValue($product['product_condition']).' '.$product_name['data'].' '.$product['product_name'];

			Db::table('product')
				->where(['id'=>$product['id']])
				->update(['full_name' => $full_name]);
		}

		return 'ok';
	}

	public function getProductConditionValue($key)
    {
        $product_fineness = Db::table('product_fineness')->where(['id' => $key])->field('value')->find();
        return $product_fineness['value'];
    }

    public function set_product_name($systematics_id = 0){
        $systematics_arr = $this->get_systematics($systematics_id);
        if(array_key_exists('xilie', $systematics_arr)){
            $product_name = $systematics_arr['brand']['title'].'/'.$systematics_arr['xilie']['title'];
        }elseif(array_key_exists('brand', $systematics_arr)){
            $product_name = $systematics_arr['brand']['title'];
        }else{
            $product_name = '';
        }
        $date = array('code'=>666,'msg'=>'产品名称生成成功','data'=>$product_name);
        return $date;
    }

    public function get_systematics($systematics)
    {
        $row = Db::table('systematics')->where('id',$systematics)->field('id,superior_id,superior_gene,title')->find();

        if($row != null)
        {
            if($row['superior_gene'] == 0){
                $date['systematics']['id'] = $systematics;
                $date['systematics']['title'] = $row['title'];
            }else{
                $superior_gene_arr = explode('-',$row['superior_gene']);
                if(count($superior_gene_arr) == 1){
                    $date['systematics']['id'] = $row['superior_id'];
                    $title = Db::table('systematics')->where('id',$row['superior_id'])->field('title')->find();
                    $date['systematics']['title'] = $title['title'];

                    $date['child_systematics']['id'] = $systematics;
                    $date['child_systematics']['title'] = $row['title'];
                }elseif(count($superior_gene_arr) == 2){
                        $date['systematics']['id'] = $superior_gene_arr[0];
                        $title = Db::table('systematics')->where('id',$superior_gene_arr[0])->field('title')->find();
                        $date['systematics']['title'] = $title['title'];

                        $date['child_systematics']['id'] = $superior_gene_arr[1];
                        $title = Db::table('systematics')->where('id',$superior_gene_arr[1])->field('title')->find();
                        $date['child_systematics']['title'] = $title['title'];

                        $date['brand']['id'] = $systematics;
                        $date['brand']['title'] = $row['title'];
                }elseif(count($superior_gene_arr) == 3){
                    $date['systematics']['id'] = $superior_gene_arr[0];
                    $title = Db::table('systematics')->where('id',$superior_gene_arr[0])->field('title')->find();
                    $date['systematics']['title'] = $title['title'];
                    
                    $date['child_systematics']['id'] = $superior_gene_arr[1];
                    $title = Db::table('systematics')->where('id',$superior_gene_arr[1])->field('title')->find();
                    $date['child_systematics']['title'] = $title['title'];
                    
                    $date['brand']['id'] = $superior_gene_arr[2];
                    $title = Db::table('systematics')->where('id',$superior_gene_arr[2])->field('title')->find();
                    $date['brand']['title'] = $title['title'];
                    
                    $date['xilie']['id'] = $systematics;
                    $date['xilie']['title'] = $row['title'];
                }
            }

        }else{
            $date = [];
        }

        return $date;
    }

}